package com.luxsoft.em2.replication.local;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.listener.DefaultMessageListenerContainer;

import com.luxsoft.siipap.util.DBUtils;
import com.luxsoft.utils.LoggerHelper;

/**
 * 
 * @author Ruben Cancino 
 *
 */
public class ReplicadorLocalManager {
	
	public static  ReplicadorLocalManager INSTANCE;
	
	Logger logger=LoggerHelper.getLogger();
	
	ApplicationContext context;
	
	private ReplicadorLocalManager(){
		initContext();
	}
	
	private void initContext(){
		System.out.println("Conectado a:");
		DBUtils.whereWeAre();
		logger.info("Inicianlizando spring context para replicacion JMS en sucursal:");
		context=new ClassPathXmlApplicationContext(
				new String[]{
						"config/local/jms-replica-localManager-context.xml"
						}
				);
		
	}
	
	public List<String[]> getBrokers(){
		List<String[]> brokers=new ArrayList<String[]>();
		Map<String,Object> beansMap=context.getBeansOfType(DefaultMessageListenerContainer.class);
		Set<Map.Entry<String,Object>> set=beansMap.entrySet();
		for(Map.Entry<String, Object> entry:set){
			DefaultMessageListenerContainer c=(DefaultMessageListenerContainer)entry.getValue();
			ActiveMQConnectionFactory fac=(ActiveMQConnectionFactory)c.getConnectionFactory();
			brokers.add(new String[]{entry.getKey(),fac.getBrokerURL()});
		}
		return brokers;
	}
	
	public static ReplicadorLocalManager getInstance(){
		if(INSTANCE==null){
			INSTANCE=new ReplicadorLocalManager();
		}
		return INSTANCE;
	}
	
	public static void main(String[] args) throws Exception{
		ReplicadorLocalManager manager=getInstance();
		manager.getBrokers();
	}

}
